# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=awstats
pkgver=7.9
pkgrel=0
pkgdesc="Free real-time logfile analyzer to get advanced statistics"
url="https://awstats.sourceforge.net/"
arch="noarch"
license="GPL-3.0-or-later"
depends="perl perl-uri"
subpackages="$pkgname-doc"
options="!check" # no testsuite
source="https://prdownloads.sourceforge.net/awstats/awstats-$pkgver.tar.gz"

# secfixes:
#   7.9-r0:
#     - CVE-2022-46391
#   7.8-r1:
#     - CVE-2020-35176
#   7.6-r2:
#     - CVE-2017-1000501

prepare() {
	local file
	default_prepare
	for file in tools/* wwwroot/cgi-bin/*; do
		[ -f "$file" ] || continue
		sed -e "s:/usr/local/$pkgname/wwwroot:/usr/lib/$pkgname:g" \
			-i "$file"
	done
	for file in wwwroot/cgi-bin/*; do
		[ -f "$file" ] || continue
		sed -e "s:/usr/share/$pkgname:/usr/lib/$pkgname/cgi-bin:g" \
			-i "$file"
	done
}

package() {
	install -d "$pkgdir"/usr/share/doc/$pkgname \
		"$pkgdir"/var/lib/$pkgname \
		"$pkgdir"/usr/lib/$pkgname \
		"$pkgdir"/etc/$pkgname \
		"$pkgdir"/usr/bin

	# docs
	cp -a docs tools/xslt "$pkgdir"/usr/share/doc/$pkgname

	# cgi-bin
	cp -a wwwroot/* "$pkgdir"/usr/lib/$pkgname
	mv "$pkgdir"/usr/lib/$pkgname/cgi-bin/$pkgname.model.conf \
		"$pkgdir"/etc/awstats/
	ln -s /etc/$pkgname/$pkgname.model.conf \
		"$pkgdir"/usr/lib/$pkgname/cgi-bin/$pkgname.model.conf

	# tools
	cp tools/*.pl "$pkgdir"/usr/bin/
	ln -s /usr/lib/$pkgname/cgi-bin/$pkgname.pl "$pkgdir"/usr/bin/

	# remove java sources and example plugin
	rm -fr "$pkgdir"/usr/lib/$pkgname/classes/src \
		"$pkgdir"/usr/lib/$pkgname/cgi-bin/plugins/example
}

sha512sums="
1270c7486a5558a54abc7b43cd92ffcea356ff4c00271bb824758ef4f5736eff559dd38eb7bc7bff8d45efa970a60ddcc5771762a3c3a32f003b0e39c55814d9  awstats-7.9.tar.gz
"
